package com.uniriho.lxr0811_7300_bl.ui.fingerinput

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.uniriho.lxr0811_7300_bl.base.Constant
import com.uniriho.lxr0811_7300_bl.databinding.FragmentFingerinputBinding

class Fragment_FingerInput : Fragment(), View.OnClickListener {
    private lateinit var textView: TextView
    private lateinit var powerOnBtn: Button
    private lateinit var powerOffBtn: Button
    private lateinit var vsBtn: Button
    private lateinit var inputBtn: Button
    private lateinit var comBtn: Button
    private lateinit var clearBtn: Button
    private lateinit var setSecurityLevelBtn: Button
    private lateinit var readSecurityLevelBtn: Button
    private var fingerInputBinding: FragmentFingerinputBinding? = null

    private val viewModelFingerInput by lazy {
        ViewModelProvider(this).get(ViewModel_FingerInput::class.java)
    }

    // This property is only valid between onCreateView and
    // onDestroyView.

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        fingerInputBinding = FragmentFingerinputBinding.inflate(inflater, container, false)
        val root: View = fingerInputBinding!!.root

        initView()
        initData()
        initEvent()
        return root
    }

    private fun initEvent() {
        textView.setOnClickListener(this)
        powerOnBtn.setOnClickListener(this)
        powerOffBtn.setOnClickListener(this)
        vsBtn.setOnClickListener(this)
        inputBtn.setOnClickListener(this)
        comBtn.setOnClickListener(this)
        clearBtn.setOnClickListener(this)
        setSecurityLevelBtn.setOnClickListener(this)
        readSecurityLevelBtn.setOnClickListener(this)
    }

    private fun initData() {

    }

    private fun initView() {
        textView = fingerInputBinding!!.textResult
        powerOnBtn = fingerInputBinding!!.btnFingerPowON
        powerOffBtn = fingerInputBinding!!.btnFingerPowOff
        vsBtn = fingerInputBinding!!.btnFingerGetVS
        inputBtn = fingerInputBinding!!.btnFingerInput
        comBtn = fingerInputBinding!!.btnFingerCom
        clearBtn = fingerInputBinding!!.btnFingerClear
        setSecurityLevelBtn = fingerInputBinding!!.btnFingerSetLevel
        readSecurityLevelBtn = fingerInputBinding!!.btnFingerReadLevel
        viewModelFingerInput.result.observe(viewLifecycleOwner) {
            textView.text = it
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        fingerInputBinding = null
    }

    override fun onClick(p0: View?) {

        when (p0) {

            powerOnBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_POWERON)
            }

            powerOffBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_POWEROFF)
            }

            vsBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_VS)
            }

            inputBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_INPUT)
            }

            comBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_COMPARISON)
            }

            clearBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_CLEAR)
            }

            setSecurityLevelBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_SETSECURITYLEVEL)
            }

            readSecurityLevelBtn -> {
                viewModelFingerInput.sendData(Constant.FINGERINPUT_READSECURITYLEVEL)
            }

        }
    }
}